package com.tms.springboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.tms.springboot.base.pojo.*;
import com.tms.springboot.search.PdTruckSearch;
import com.tms.springboot.service.*;
import com.tms.springboot.utils.UUId;
import com.tms.springboot.view.PdTruckBasicsView;
import com.tms.springboot.view.PdTruckView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.rmi.CORBA.Util;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 车辆信息表 前端控制器
 * </p>
 *
 * @author cjj
 * @since 2023-06-14
 */
@RestController
@RequestMapping("/pd-truck")
public class PdTruckController {

    @Autowired
    private PdTruckService pdTruckService;

    @Autowired
    private PdTruckLicenseService pdTruckLicenseService;

    @Autowired
    private PdTransportTripsTruckDriverService pdTransportTripsTruckDriver;

    /**
     * 分页查询车辆信息列表
     * @param pdTruckSearch
     * @return
     * @throws Exception
     */
    @RequestMapping("/truckPage")
    public PageInfo<PdTruckView> pdTruckPage(@RequestBody PdTruckSearch pdTruckSearch) throws Exception {

        PageInfo<PdTruckView> pdTruckViewPageInfo = this.pdTruckService.pdTruckPage(pdTruckSearch);

        return pdTruckViewPageInfo;

    }

    /**
     * 新增车辆信息
     * @param pdTruck
     * @return
     */
    @RequestMapping("/saveTruck")
    public boolean saveTruck(@RequestBody PdTruck pdTruck){

        pdTruck.setId(UUId.nextId());

        pdTruck.setStatus(0);

        boolean save = this.pdTruckService.save(pdTruck);

        PdTruckLicense pdTruckLicense = new PdTruckLicense();

        pdTruckLicense.setId(UUId.nextId());

        pdTruckLicense.setTruckId(pdTruck.getId());

        if (save){

            boolean save1 = this.pdTruckLicenseService.save(pdTruckLicense);

            return save1;

        }

        return false;

    }

    /**
     * 车辆详情查询
     * @param pdTruck
     * @return
     * @throws Exception
     */
    @RequestMapping("/selectVehicledetails")
    public List<PdTruckBasicsView> vehicledetails(@RequestBody PdTruck pdTruck) throws Exception {

        List<PdTruckBasicsView> pdTruckBasicsViews = this.pdTruckService.vehicledetailsService(pdTruck);

        return pdTruckBasicsViews;

    }

    /**
     * 编辑车辆信息
     * @param pdTruckBasicsView
     * @return
     * @throws Exception
     */
    @RequestMapping("/updateTruck")
    public boolean updateTruck(@RequestBody PdTruckBasicsView pdTruckBasicsView) throws Exception {

        PdTruck pdTruck = new PdTruck();
        pdTruck.setId(pdTruckBasicsView.getTruckId());
        pdTruck.setLicensePlate(pdTruckBasicsView.getLicensePlate());

        boolean b = this.pdTruckService.updateTruckService(pdTruck);

        return b;

    }

    /**
     * 删除车辆
     * @param pdTruck
     * @return
     */
    @RequestMapping("/deleteTruck")
    public boolean deleteTruck(@RequestBody PdTruck pdTruck){

        boolean remove = this.pdTruckService.removeById(pdTruck.getId());

        if(remove){

            boolean truck_id = this.pdTruckLicenseService.remove(new QueryWrapper<PdTruckLicense>().eq("truck_id", pdTruck.getId()));

            System.out.println(truck_id);

            if(truck_id){

                PdTransportTripsTruckDriver byId = this.pdTransportTripsTruckDriver.getById(pdTruck.getId());

                if(byId!=null && !byId.equals("")){

                    boolean remove2 = this.pdTransportTripsTruckDriver.removeById(pdTruck.getId());

                    return remove2;

                }else{

                    return true;
                }

            }

        }

        return false;

    }

}
